<template>
  <div>
    <a-card :bordered="false" size="default" title="用户列表">
      <div class="table-operator">
        <a-button type="primary" class="btn-success" icon="plus" @click="handleAdd()"> 新增 </a-button>
      </div>
      <a-spin :spinning="loading">
        <a-table
          ref="table"
          :columns="columns"
          :dataSource="data"
          :pagination="pagination"
          @change="handleTableChange"
          rowKey="id"
        >
          <span slot="fullName" slot-scope="text">
            <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="authorities" slot-scope="text">
            <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="operate" slot-scope="text, record">
            <template>
              <a @click="handleCheck(record)">详情</a>
              <a-divider type="vertical" />
              <a
                @click="handleEdit(record)"
                v-if="record.login != 'admin' && record.login != 'system' && record.login != 'anonymoususer'"
              >编辑</a
              >
              <a-divider
                type="vertical"
                v-if="record.login != 'admin' && record.login != 'system' && record.login != 'anonymoususer'"
              />
              <a
                class="text-danger"
                @click="handleDelete(record)"
                v-if="record.login != 'admin' && record.login != 'system' && record.login != 'anonymoususer'"
              >删除</a
              >
              <a
                class="text-danger text-disabled"
                v-if="record.login === 'admin' || record.login === 'system' || record.login === 'anonymoususer'"
              >删除</a
              >
            </template>
          </span>
        </a-table>
      </a-spin>
      <check-modal ref="checkModal" @ok="handleOk" />
      <create-form ref="createForm" :tRoleList="tRoleLists" @ok="handleOk" />
      <edit-modal ref="editModal" :tRoleList="tRoleLists" @ok="handleOk" />
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import { Ellipsis } from '@/components'
import CheckModal from './modules/user/checkModal'
import CreateForm from './modules/user/createForm'
import EditModal from './modules/user/editModal'
import { getRoutePermission } from '@/utils/util'
import { getUsersManageList, deleteUsersManage } from '@/api/user'
import { getRolesList } from '@/api/role'

export default {
  name: 'UsersManage',
  components: {
    Ellipsis,
    CheckModal,
    CreateForm,
    EditModal
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,

      radioTime: 1, // 默认选中
      data: [],
      interfaceName: [],
      interfaceNameList: [
        { name: 'IOC', checked: false },
        { name: 'IPC', checked: true },
        { name: 'IFC', checked: false }
      ],
      templateDatas: [], // 接收下拉框数据的对象
      isAdd: false,
      isEditor: false,
      isDetails: false,
      isDelete: false,
      // checked1: true,
      // checked2: false,
      // checked3: false,
      tags: [],
      selectedTags: [],
      // 角色信息
      tRoleLists: [],
      info: {},

      // form: this.$form.createForm(this),
      loading: true,
      visible: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: null,
      mdl: {},
      routerPermission: {},
      // 分页属性
      pagination: {
        defaultPageSize: 10,
        pageNo: 0,
        pageSize: 10, // 默认每页显示数量
        showSizeChanger: true, // 显示可改变每页数量
        showQuickJumper: true, // 是否可以快速跳转至某页
        pageSizeOptions: ['10', '20', '50', '100'], // 每页数量选项
        showTotal: (total) => this.$t('pagination.total') + ` ${total} ` + this.$t('pagination.items'), // 分页中显示总的数据
        total: 0 // 总条数
      },
      // 查询参数
      queryParam: {}
    }
  },
  created () {
    // 表头初始国际化
    this.columns = [
      {
        title: 'ID',
        dataIndex: 'id',
        width: 100,
        scopedSlots: { customRender: 'id' },
        sorter: (a, b) => {
          return a.id.localeCompare(b.id)
        } // 排序设置
      },
      {
        title: '用户名',
        dataIndex: 'login'
      },
      {
        title: '姓名',
        dataIndex: 'userName',
        width: 100,
        scopedSlots: { customRender: 'userName' }
      },
      {
        title: '角色',
        dataIndex: 'tRole.name'
      },
      {
        title: '联系电话',
        dataIndex: 'phone',
        width: 100,
        scopedSlots: { customRender: 'phone' }
      },
      {
        title: '邮箱',
        dataIndex: 'email',
        width: 100,
        scopedSlots: { customRender: 'email' }
      },
      {
        title: '注册日期',
        dataIndex: 'createdDateLocal',
        scopedSlots: { customRender: 'createdDateLocal' }
      },
      {
        title: '状态',
        dataIndex: 'activated',
        width: 150,
        customRender: (text, row, index) => {
          switch (text) {
            case '0':
              return (
                <div>
                  <a-badge status="error" />
                  <span>禁用</span>
                </div>
              )
            case '1':
              return (
                <div>
                  <a-badge status="success" />
                  <span>正常</span>
                </div>
              )
            default:
              return text
          }
        }
      },
      {
        title: '操作',
        width: '200px',
        dataIndex: 'operate',
        scopedSlots: { customRender: 'operate' }
      }
    ]
  },

  mounted () {
    console.log('xxxx====', this.userPermission)
    // 拿路由信息 this.$route.name

    this.getUsersManageList()
    // this.getPermission()
  },

  methods: {
    // 显示隐藏
    // toggleAdvanced () {
    //   this.advanced = !this.advanced
    // },
    // this.$store.commit('GetInfo'),
    // console.log(this.$store.commit('GetInfo')),
    // 查看
    handleCheck (record) {
      this.$refs.checkModal.check(record)
    },
    // 新增
    handleAdd () {
      this.$refs.createForm.add()
      this.getRolesList()
    },

    // 编辑
    handleEdit (record) {
      console.log('编辑record-------', record)
      this.$refs.editModal.edit(record) // $refs指弹出框的所有方法，edit只是modal框的一个方法
      this.getRolesList()
    },

    // 删除
    handleDelete (record) {
      this.$confirm({
        title: '提示',
        content: `你确认删除 ${record.login} 用户吗?`,
        okText: '确定',
        icon: 'home',
        okType: 'primary',
        cancelText: '取消',
        onOk: () => {
          deleteUsersManage({ usersId: record.id }).then((res) => {
            // 存储成功、刷新列表
            this.$notification['success']({
              message: '提示',
              description: '数据删除成功'
            })
            if (this.data.length === 1) {
              this.pagination.pageNo = this.pagination.pageNo - 1
            }
            this.getUsersManageList()
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    },

    handleChange (tag, checked) {
      const { selectedTags } = this
      const nextSelectedTags = checked ? [...selectedTags, tag] : selectedTags.filter((t) => t !== tag)
      console.log('You are interested in: ', nextSelectedTags)
      this.selectedTags = nextSelectedTags
    },

    handleCancel () {
      this.visible = false
    },

    // 获取列表
    getUsersManageList () {
      const filter = { sort: 'id,desc', page: this.pagination.pageNo, size: this.pagination.pageSize }
      this.loading = true
      getUsersManageList(filter)
        .then((response) => {
          response.data.forEach((item) => {
            item.activated = JSON.stringify(item.activated)
            item.modifyDate = moment(item.modifyDate).format('YYYY-MM-DD HH:mm:ss')
          })
          this.loading = false
          this.data = response.data
          this.pagination.total = Number(response.totalCount)
        })
        .catch((error) => {
          console.log(error)
        })
    },

    // 获取角色信息
    getRolesList () {
      const filter = { page: 0, size: 100 }
      getRolesList(filter)
        .then((response) => {
          response.data.forEach((item) => {
            item.createDate = moment(item.createDate).format('YYYY-MM-DD HH:mm:ss')
            item.modifyDate = moment(item.modifyDate).format('YYYY-MM-DD HH:mm:ss')
          })
          this.tRoleLists = response.data
          console.log('角色列表----', this.tRoleLists)
        })
        .catch((error) => {
          console.log(error)
        })
    },

    // 更新列表
    handleOk () {
      // this.$refs.table.refresh()
      this.getUsersManageList()
    },
    // 分页方法
    handleTableChange (pagination) {
      console.log('分页事件-----------', pagination)
      this.pagination.pageNo = pagination.current - 1
      this.pagination.pageSize = pagination.pageSize
      // if (this.selectedTags.length === 0) {
      //   this.getDictionariesList()
      // } else {
      //   this.refresh()
      // }
      this.getUsersManageList()
    },

    interfaceNameChange (item) {
      console.log(this.interfaceNameList)
      console.log(this.interfaceName)
    },

    // 获取当前页面的权限
    getPermission () {
      this.routerPermission = getRoutePermission(this.$route.name)
      console.log('用户信息----', this.routerPermission)
    }
  }
}
</script>

<style>
.ant-card-body {
  padding: 20px !important;
}
.antd-pro-components-standard-form-row-index-label > span {
  width: 150px;
  height: auto !important;
  border-right: 3px solid #1890ff;
  background: #e6f7ff;
}
.table-page-search-wrapper .ant-form-inline .ant-form-item .ant-form-item-control-wrapper {
  margin-top: 5px !important;
}
.table-page-search-wrapper .ant-form-inline .ant-form-item .ant-form-item-label {
  margin-top: 5px !important;
}
</style>
<style lang="less" scoped>
.table-operator {
  margin-bottom: 10px;
}
/deep/ .ant-card-head {
  padding: 0 20px;
}
.table-page-search-wrapper .ant-form-inline .ant-form-item {
  margin-bottom: 0;
}
.ant-pro-components-tag-select {
  /deep/ .ant-pro-tag-select .ant-tag {
    margin-right: 24px;
    padding: 0 8px;
    font-size: 14px;
  }
}
.ant-pro-pages-list-projects-cardList {
  margin-top: 24px;

  /deep/ .ant-card-meta-title {
    margin-bottom: 4px;
  }

  /deep/ .ant-card-meta-description {
    height: 44px;
    overflow: hidden;
    line-height: 22px;
  }

  .cardItemContent {
    display: flex;
    height: 20px;
    margin-top: 16px;
    margin-bottom: -4px;
    line-height: 20px;

    > span {
      flex: 1 1;
      color: rgba(0, 0, 0, 0.45);
      font-size: 12px;
    }

    /deep/ .ant-pro-avatar-list {
      flex: 0 1 auto;
    }
  }
}
</style>
<style lang="less" scoped>
.checkable {
  margin-bottom: 10px;
  padding: 5px 0;
  border-bottom: 1px dashed #e8e8e8;
  > span {
    float: left;
    padding-right: 5px;
    width: 100px;
    color: #888;
    height: auto !important;
  }
  .checkable-right {
    margin-left: 100px;
    .ant-tag-checkable {
      color: #333;
    }
    .ant-tag-checkable-checked {
      color: #fff;
    }
  }
}
.search-btn-handle {
  .table-page-search-submitButtons {
    float: right;
  }
}
.ant-table-default {
  background: #f00 !important;
  .ant-table-content {
    .ant-table-thead > tr > th > {
      padding: 10px 16px;
    }
    .ant-table-tbody > tr > td > {
      padding: 10px 16px;
    }
  }
}
.text-disabled {
  color: rgba(0, 0, 0, 0.25);
}
</style>
